const path = require("path");
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
const CssMinimizerPlugin = require("css-minimizer-webpack-plugin")
//配置html
const HtmlWebpackPlugin = require('html-webpack-plugin')
//配置esLint
// const ESLintPlugin = require('eslint-webpack-plugin')
const CopyWebpackPlugin = require("copy-webpack-plugin")

module.exports = {
  entry: path.resolve(__dirname, "../src/index.js"),
  output: {
    path: path.resolve(__dirname, "../build"),
    filename: "./js/bundle.js"
  },
  module: {
    rules: [
      {
        //配置css
        test: /\.css$/i,
        use: [MiniCssExtractPlugin.loader, "css-loader"]
      },
      {
      //  配置less
        test:/\.less$/i,
        use:[MiniCssExtractPlugin.loader, "css-loader","less-loader"]
      },
      {
        test: /\.(png|jpg|gif|svg)$/,
        //asset 在导出一个 data URI 和发送一个单独的文件之间自动选择。代替之前的url-loader和file-loader
        type: "asset",
        parser: {
          dataUrlCondition: {
            maxSize: 15 * 1024, // 小于15kb以下的图片会被打包成base64格式
          },
        },
        generator: {
          filename: 'images/[name][ext]'
        }
      }
    ]
  },
  plugins: [
    new MiniCssExtractPlugin({
    //输出独立文件的文件名
    filename: './css/index.css'
  }),
    new HtmlWebpackPlugin({//配置HTML
      template:'./public/index.html',
      filename:'main.html',
      minify: {
        collapseWhitespace: true,
        keepClosingSlash: true,
        removeComments: true,
        removeRedundantAttributes: true,
        removeScriptTypeAttributes: true,
        removeStyleLinkTypeAttributes: true,
        useShortDoctype: true
      }
    }),
    // new ESLintPlugin({//配置esLint规则
    //   fix:true
    // }),
    new CopyWebpackPlugin({
      //配置公共文件复制
      patterns: [{
        from: "./public",
        to: "public",
        //忽略html文件
        globOptions: {
          ignore: ["**/index.html"],
        }
      }],
    })],
  optimization: {
    minimizer: [
      new CssMinimizerPlugin(),
    ],
    minimize: true,
  },
  mode: "production"
}